<!DOCTYPE HTML>
<html lang="cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <%include("/common/header.html",{title:''}){}%>
    <title>商品列表</title>
    <link rel="stylesheet" href="css/formSelects-v4.css" />
</head>
<body>

<div class="x-nav" style="margin: 20px auto auto 15px;">
      <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
        <a href="javascript:;">首页</a><span lay-separator="">/</span>
        <a href="javascript:;">销售端</a><span lay-separator="">/</span>
        <a><cite>销售员列表</cite></a>
      </span>
    <a class="layui-btn layui-btn-primary layui-btn-small" style="margin: -5px 14px 0px 0px;float:right;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh-3"></i>
    </a>
</div>


<div class="x-body" style="margin: 20px auto auto 15px;">
    <div class="layui-row" >
        <div class="layui-input-inline">
            <input type="text" id="search_term"  placeholder="请输入内容" class="layui-input" >
        </div>
        <div class="layui-input-inline">
            <button  class="layui-btn layui-btn-checked" lay-submit="" lay-filter="sreach" id="searchBtn" > <i class="layui-icon layui-icon-search"></i>查询</button>
        </div>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
            <script type="text/html" id="toolbarDemo">
                <button class="layui-btn layuiadmin-btn-useradmin" lay-event="add">开设销售员账号</button>
                <!--<button class="layui-btn layuiadmin-btn-useradmin" lay-event="combination">组合商品</button>-->
            </script>
            <script type="text/html" id="imgTpl">
                <img style="display: inline-block; width: 50%; height: 100%;" src={{ d.avatar }}>
            </script>
            <script type="text/html" id="table-useradmin-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>
<!-- 单个商品弹出页面 -->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin"
     style="padding: 15px;display: none">
    <form action class="layui-form" lay-filter="component-form-element" id="myform">
        <input type="hidden" name="id">
        <input type="hidden" name="agent_id" id="agent_id">
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">姓名:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="name" lay-verify="required" value=""  class="layui-input"
                       placeholder="请输入姓名">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">性别:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <select name="sex">
                    <option value="0">男</option>
                    <option value="1">女</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">手机:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" maxlength="11" lay-verify="phone" class="layui-input"
                       placeholder="请输入手机号码">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">身份证号:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="idCard" lay-verify="idCard" maxlength="18"  class="layui-input"
                       placeholder="请输入身份证号">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">开户行名:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="bankName" lay-verify="required" class="layui-input"
                       placeholder="请输入开户行名称">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">银行卡号:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="bankNum" lay-verify="bankNum" class="layui-input"
                       placeholder="请输入银行卡号">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">所在区域:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="address" lay-verify="required" value=""  class="layui-input"
                       placeholder="所在区域">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">用户名:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="userName" lay-verify="userName" value=""  class="layui-input"
                       placeholder="请输入用户名">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">密码:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="passWord" lay-verify="required" value=""  class="layui-input"
                       placeholder="请输入密码">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">邮箱:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="email" lay-verify="required" class="layui-input"
                       placeholder="请输入邮箱">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-element">立即提交</button>
            </div>
        </div>
    </form>
</div>

<div class="layui-form" lay-filter="layuiadmin-form-useradmins"
     id="layuiadmin-form-useradmins"
     style="padding: 15px;display: none">
    <form action class="layui-form" lay-filter="component-form-elements" id="myforms">
        <input type="hidden" name="id" id="id">
        <input type="hidden" name="agent_id">
        <input type="hidden" name="saleNumber">
        <input type="hidden" name="saleMoney">
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">姓名:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="name" lay-verify="required" value=""  class="layui-input"
                       placeholder="请输入姓名">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">性别:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <select name="sex">
                    <option value="0">男</option>
                    <option value="1">女</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">手机:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" maxlength="11" lay-verify="phone" class="layui-input"
                       placeholder="请输入手机号码">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">身份证号:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="idCard" lay-verify="idCard" maxlength="18"  class="layui-input"
                       placeholder="请输入身份证号">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">开户行名:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="bankName" lay-verify="required" class="layui-input"
                       placeholder="请输入开户行名称">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">银行卡号:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="bankNum" lay-verify="bankNum" class="layui-input"
                       placeholder="请输入银行卡号">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">所在区域:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="address" lay-verify="required" value=""  class="layui-input"
                       placeholder="所在区域">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">发展用户数:</label>
            <div class="layui-input-inline">
                <input type="number" name="userNumber" readonly="readonly" class="layui-input"
                       placeholder="发展用户数">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">用户名:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="userName" lay-verify="userName" value=""  class="layui-input"
                       placeholder="请输入用户名">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">密码:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="passWord" lay-verify="required" value=""  class="layui-input"
                       placeholder="请输入密码">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">邮箱:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="email" lay-verify="required" class="layui-input"
                       placeholder="请输入邮箱">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">注册时间:&nbsp;&nbsp;&nbsp;</label>
            <div class="layui-input-inline">
                <input type="text" name="createDate" readonly value=""  class="layui-input"
                       placeholder="">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-elements">立即提交</button>
            </div>
        </div>
    </form>
</div>

<%include("/common/foot.html"){}%>
<script type="text/html" id="demo">
    {{#  if(d.sex === 0){ }}
    <span>男</span>
    {{#  }else{ }}
    <span>女</span>
    {{#  } }}
</script>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
<script src="js/formSelects-v4.js" type="text/javascript" charset="utf-8"></script>
<script>

    layui.use(['index', 'table', 'form', 'laydate', 'layedit', 'AjaxCommon', 'AliangLayui'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var upload = layui.upload;
        var layedit = layui.layedit;
        var AliangLayui = layui.AliangLayui;
        var index;


        //富文本编辑器
        layedit = layui.layedit;

        var title = "人员信息";
        var size = ['100%', '100%']
        var findAll_url = 'system/agent/findAllSale.jhtml';
        var update_url = 'system/agent/save.jhtml';
        var delete_url = 'system/agent/deleteSaleMan.jhtml';

        var cols = [ //表头
            {checkbox: true, fixed: true},
            {field: 'name',  title: '姓名'},
            {field: 'sex',  title: '性别',templet:"#demo"},
            {field: 'idCard',  title: '身份证号'},
            {field:"phone",title: "联系方式"},
            {field: 'address',  title: '所在区域'},
            {field: 'userNumber',  title: '用户数'},
            {field: 'saleNumber',  title: '销售总套数'},
            {field: 'saleMoney',  title: '总销售额'},
            {field: 'createDate',  title: '注册时间'},
            {title: '操作',  toolbar: '#table-useradmin-webuser', fixed: 'right'}
        ];

        var keyWord;
        $('#searchBtn').on('click', function () {
            keyWord = $('#search_term').val();
            if (""==keyWord||undefined==keyWord){
                window.location.reload();
            }else{
                table.reload('listReload', {
                    url: findAll_url,
                    where: {
                        keyWord: keyWord
                    },
                    page: {
                        curr: 1//重新从第 1 页开始
                    }
                });
            }
        });

        AliangLayui.createTable(
            "#LAY-user-manage",
            "listReload",
            "#toolbarDemo",
            title,
            findAll_url,
            true,
            [10, 15, 20, 25],
            ['filter', 'print', 'exports'],
            cols,
            null
        );

        //自定义验证规则
        form.verify({
            required: [/[\S]+/, "必填项不能为空"],
            image: function(value){
                if(value==""){
                    return '您还未上传图片信息';
                }
            }

        });
        //初始化修改页面
        function loadData(data) {
            var ret = {
                "id": data.id,
                "userName": data.userName,
                "passWord": data.passWord,
                "address": data.address,
                "sex": data.sex,
                "birthTime":data.birthTime,
                "userNumber":data.userNumber,
                "name":data.name,
                "agent_id":data.agent_id,
                "createDate":data.createDate,
                "phone":data.phone,
                "idCard":data.idCard,
                "bankName":data.bankName,
                "bankNum":data.bankNum,
                "email":data.email,
                "saleMoney":data.saleMoney,
                "saleNumber":data.saleNumber
            }
            $("#demo").each(function() {
                // this代表的是<option></option>，对option再进行遍历
                $(this).children("option").each(function() {
                    // 判断需要对那个选项进行回显
                    if (this.value == data.sex) {
                        console.log($(this).text());
                        // 进行回显
                        $(this).attr("selected","selected");
                    }
                });
            })
            return ret;
        }
        //时间范围
        laydate.render({
            elem: '#test9'
            ,type: 'time'
            ,range: true
        });

        //初始化日期模块
        laydate.render({
            elem: '#create' //指定元素
        });
        //弹出修改页面调用初始化数据方法
        function initUpdate(data) {
            layer.open({
                title: title,
                area: size,
                type: 1,
                content: $('#layuiadmin-form-useradmin'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
            });

            form.verify({
                userName: function(value){
                    if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
                        return '用户名不能有特殊字符';
                    }
                    if(/(^\_)|(\__)|(\_+$)/.test(value)){
                        return '用户名首尾不能出现下划线\'_\'';
                    }
                    if(/^\d+\d+\d$/.test(value)){
                        return '用户名不能全为数字';
                    }
                    var checkResult = "";
                    var checkValue=value.trim();
                    $.ajax({
                        url: "account/validateUserName.jhtml",
                        type: "post",
                        data: {
                            "userName":checkValue
                        },
                        async: false,
                        success: function(result) {
                            // alert(JSON.stringify(result))
                            if(result.obj.length>0){
                                checkResult = "该账号已存在";
                            }
                        },
                        error: function() {
                        }
                    });
                    return checkResult;
                },
                idCard:function (value) {
                    // if(!new RegExp("/(^\\d{15}$)|(^\\d{17}(x|X|\\d)$)/").test(value)){
                    //     return '请输入正确的身份证号';
                    // }
                    var checkResult = "";
                    var checkValue=value.trim();
                    $.ajax({
                        url: "account/validateIdCard.jhtml",
                        type: "post",
                        data: {
                            "idCard":checkValue
                        },
                        async: false,
                        success: function(result) {
                            // alert(JSON.stringify(result))
                            if(result.obj.length>0){
                                checkResult = "该身份证号已存在";
                            }
                        },
                        error: function() {
                        }
                    });
                    return checkResult;
                },
                phone:function(value){
                    // if(!new RegExp("/^1\\d{10}$/").test(value)){
                    //     return '请输入正确的手机号';
                    // }
                    var checkResult = "";
                    var checkValue=value.trim();
                    $.ajax({
                        url: "account/validatePhone.jhtml",
                        type: "post",
                        data: {
                            "phone":checkValue
                        },
                        async: false,
                        success: function(result) {
                            // alert(JSON.stringify(result))
                            if(result.obj.length>0){
                                checkResult = "该手机号码已存在";
                            }
                        },
                        error: function() {
                        }
                    });
                    return checkResult;
                },
                bankNum:function (value) {
                    var checkResult = "";
                    var checkValue=value.trim();
                    $.ajax({
                        url: "validate/validateBankNum.jhtml",
                        type: "post",
                        data: {
                            "bankNum":checkValue
                        },
                        async: false,
                        success: function(result) {
                            // alert(JSON.stringify(result))
                            if(result.obj.length>0){
                                checkResult = "请换一张银行卡";
                            }
                        },
                        error: function() {
                        }
                    });
                    return checkResult;
                },
                required: [/[\S]+/, "必填项不能为空"]
            });

            $('#myform')[0].reset();
            var agentId=${session.om_user.id};
            // alert(agentId)
            $("#agent_id").val(agentId);
        }

        function initUpdates(data) {
            layer.open({
                title: title,
                area: size,
                type: 1,
                content: $('#layuiadmin-form-useradmins'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
            });
            layui.form.val("component-form-elements", loadData(data));
        }
        form.on('submit(component-form-elements)', function (data) {
            data.field.content = layedit.getContent(index);
            // alert(JSON.stringify(data.field))
            postUpdate(data);
            return false;
        });

        //修改页面的内容提交到后台处理
        form.on('submit(component-form-element)', function (data) {
            data.field.content = layedit.getContent(index);
            // alert(JSON.stringify(data.field))
            postUpdate(data);
            return false;
        });
        //头工具栏事件
        table.on('toolbar(LAY-user-manage)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    initUpdate(0);
                    break;
                case 'combination':
                    initUpdates(0);
                    break;
                case 'export':
                    layer.msg("导出");
                    break;
            }
        });

        //点击每行右工具栏“操作”里面按钮效果
        table.on('tool(LAY-user-manage)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'edit') {
                // if (data.combination!=""){
                //     initUpdates(data);
                // }else{
                initUpdates(data);
                // }

            } else if (obj.event === 'del') {
                layer.confirm('删除以后数据将不能恢复，确定删除吗？', function (index) {
                    //alert(data.id);
                    postDelete(data.id);
                });
            }
        });


        //监听搜索按钮效果
        form.on('submit(LAY-user-front-search)', function (data) {
            var field = data.field;
            table.reload('listReload', {
                where: {
                    name: field.name,
                    price: field.price,
                    pay: field.pay
                },
                page: {
                    curr: 1
                }
            });
        });

        //ajax提交删除内容
        window.postDelete = function (id) {
            $.ajax({
                url: delete_url,
                type: "POST",
                data: {
                    "id": id
                },
                dataType: "json",
                success: function (ret) {
                    if (ret == null) {
                        layer.msg("删除失败", {
                            icon: 5
                        });
                    } else {
                        layer.msg("删除成功", {
                            icon: 6
                        });
                        layer.closeAll();
                        table.reload('listReload');
                    }
                    return ret;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function: postDelete()";
                    layer.open({
                        title: "错误信息",
                        area: ['600px', '600px'],
                        type: 2,
                        content: "base/error.jhtml?info=" + estr, //
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        }

        //ajax提交修改内容
        window.postUpdate = function (data) {
            var formData = new FormData();
            formData.append("data", JSON.stringify(data.field));
            $.ajax({
                url: update_url,
                type: "POST",
                //traditional:true,
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        layer.msg(data.msg, {time: 600, icon: 1}, function () {
                            //table.reload('listReload');

                        });
                        location.replace(location.href);
                    } else {
                        layer.msg(data.msg, {icon: 6});
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
                    layer.open({
                        title: "错误信息",
                        type: 2,
                        content: "base/error.jhtml?info=" + estr,
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        }
    });

</script>
</body>

</html>